<?php
/* 
 *out of stock orders
 */
class OutStockAction extends CommonAction {
    public function _filter(&$map) {
        if( $_REQUEST['remark'] != '' )
          $map['remark'] = trim($_REQUEST['remark']);

        if( $_REQUEST['logino'] != '' )
          $map['logino'] = array('like',"%".trim($_REQUEST['logino'])."%");

        if( $_REQUEST['phone'] != '' )
          $map['phone'] = array('like',"%".trim($_REQUEST['phone'])."%");

        if( $_REQUEST['mobile'] != '' )
          $map['mobile'] = array('like',"%".trim($_REQUEST['mobile'])."%");

        if( $_REQUEST['address'] != '' )
          $map['address'] = array('like',"%".trim($_REQUEST['address'])."%");

        if( $_REQUEST['shipping_name'] != '' )
          $map['shipping_name'] = array('like',"%".trim($_REQUEST['shipping_name'])."%");

        if( $_REQUEST['customer_name'] != '' )
          $map['customer_name'] = array('like',"%".trim($_REQUEST['customer_name'])."%");

        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
                $where['Sale_detail.sale_title'] = array('like',"%".$key."%");//产品名称
            $where['_logic'] = "or";
            $map['_complex'] = $where;
        }
        if(!empty($_REQUEST['order_no'])) {
            $key2 = trim($_REQUEST['order_no']);
              $map['_string'] = '(Sale.sale_no like "%'.$key2.'%")  OR ( Sale.mall_no like "%'.$key2.'%") ';
        }

        

        if( $_REQUEST['order_source_id'] != '' )
          $map['order_source_id'] = $_REQUEST['order_source_id'];
        //time 
        $cond_time = array();
        if( $_REQUEST['start_time'] != '' )
          array_push($cond_time, array( 'egt' , strtotime($_REQUEST['start_time']) ) );
          //$cond_time = array( 'egt' , strtotime($_REQUEST['start_time']) );
        if( $_REQUEST['end_time'] != '' )
          array_push($cond_time, array( 'elt' , strtotime($_REQUEST['end_time']) ) );

        //选择了时间类型,并且时间不为空，则按时间进行搜索
        if( $_REQUEST['time_type'] != "" && 
          ($_REQUEST['end_time'] != '' ||  $_REQUEST['start_time'] != '' ) ){
            switch ($_REQUEST['time_type']) {
              case '1': //审核时间
                $map['check_time '] = $cond_time;
                break;
              case '2': //发货时间
                $map['shipping_time '] = $cond_time;
                break;
              case '3': //下单时间
                if (empty($_REQUEST['keywords'])) {
                    
                    if($_REQUEST['etype'] == 'detail' ){ //导出订单详情，需按saleView进行搜索
                      $map['Sale.create_time '] = $cond_time;
                    }else
                      $map['Sale.create_time '] = $cond_time;
                }else
                    $map['Sale.create_time '] = $cond_time;
                break;
              case '4': //支付时间
                $map['pay_time '] = $cond_time;
                break;
              default:  //按下单时间查询
                $map['Sale.create_time '] = $cond_time;
                break;
            }
            
        }

        if( $_REQUEST['order_status'] != '' )
          $map['order_status'] = $_REQUEST['order_status'];
        else
            $map['order_status'] = 2; //to shipping
        
        if( $_REQUEST['status'] != '' )
          $map['Sale.status'] = $_REQUEST['status'];
        else
            $map['Sale.status'] = 1; //checked
        $map['prepare_status'] = 2; //缺货 已打印
    }

     public function  index() {
        $map = $this->_search ();
        if (method_exists ( $this, '_filter' )) {
            $this->_filter ( $map );
        }
          if(!empty($_GET)){
            //pass parameters to export_order function
            $parameters = str_replace(__URL__.'?', '', __SELF__);
            $this->assign('parameters' ,$parameters);
          }
          $sum_number = D('Sale_detailView')->where($map)->field('sum(number) as number,count( distinct logino) as sum_logino')->find(); //统计总数量
          $this->assign('sum_overview' ,$sum_number);
          parent::index('Sale_detailView',300);
    }

public function update()
{
    A('Sale')->update();
}

public function edit(){
	parent::edit('Sale');
}

/*
//缺货登记 TODO 记录操作日志
public function out_stock_log(){
   if(empty($_POST['sale_id']) || empty( $_POST['sale_detail_id']) )
     $this->edit();    
   else{
     $sid =  M('Sale')->where('id = '.$_POST['sale_id'])->setField('prepare_status',2);//缺货己打印 
     $sdid =  M('Sale_detail')->where('id = '.$_POST['sale_detail_id'])->setField('prepare_status',2);//缺货己打印 
     if($sid && $sdid)
         $this->success("缺货登记成功！",false,0.5);
     else
         $this->error('缺货登记失败或已经登记，请重试！',false,6);
    }
   
}  */

}
?>
